package com.wangijun.yuan.core.util;

import java.text.DecimalFormat;

public class BeautifyTimeUtil {


    public static String beautifyTime(double second) {
        if (second <= 0) return "0";
        final String[] units = new String[]{"秒", "分钟", "小时"};
        int digitGroups = (int) (Math.log10(second) / Math.log10(60));
        String value;
        if (digitGroups < 3) {
            value = new DecimalFormat("#,##0.#").format(second / Math.pow(60, digitGroups)) + units[digitGroups];
        } else {
            //如果超过了小时的表达范围则，则转换为天，小时，分，秒格式
            value = secondToDate(second);
        }
        return value;
    }

    public static String secondToDate(double second) {
        long time = Double.doubleToLongBits(second);
        String strTime;
        long days = time / (60 * 60 * 24);
        long hours = (time % (60 * 60 * 24)) / (60 * 60);
        long minutes = (time % (60 * 60)) / 60;
        long seconds = time % 60;
        if (days > 0) {
            strTime = days + "天" + hours + "小时" + minutes + "分钟";
        } else if (hours > 0) {
            strTime = hours + "小时" + minutes + "分钟";
        } else if (minutes > 0) {
            strTime = minutes + "分钟" + seconds + "秒";
        } else {
            strTime = second + "秒";
        }
        return strTime;
    }


    public static void main(String[] args) {
        System.out.println(beautifyTime(1));
        System.out.println(beautifyTime(120));
        System.out.println(beautifyTime(3600));
        System.out.println(beautifyTime(286990));
        System.out.println(beautifyTime(864206));
    }
}